package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.OrderCargoDto;
import com.itheima.pinda.DTO.OrderDTO;
import com.itheima.pinda.DTO.TaskPickupDispatchDTO;
import com.itheima.pinda.DTO.entity.TaskPickupDispatch;
import com.itheima.pinda.DTO.entity.TransportOrder;
import com.itheima.pinda.enums.transportorder.TransportOrderSchedulingStatus;
import com.itheima.pinda.enums.transportorder.TransportOrderStatus;
import com.itheima.pinda.feign.CargoFeign;
import com.itheima.pinda.feign.OrderFeign;
import com.itheima.pinda.mapper.TaskPickupDispatchMapper;
import com.itheima.pinda.mapper.TransportOrderMapper;
import com.itheima.pinda.service.ITransportOrderService;
import com.itheima.pinda.utils.CustomIdGenerator;
import com.itheima.pinda.TransportOrderVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 运单服务实现类
 * </p>
 */
@Service
public class TransportOrderServiceImpl extends
        ServiceImpl<TransportOrderMapper, TransportOrder> implements ITransportOrderService {
    @Autowired
    private CustomIdGenerator idGenerator;

    @Autowired
    private TransportOrderMapper transportOrderMapper;

    @Autowired
    private TaskPickupDispatchMapper taskPickupDispatchMapper;

    @Autowired
    private OrderFeign orderFeign;

    @Autowired
    private CargoFeign cargoFeign;

    @Override
    public TransportOrder saveTransportOrder(TransportOrder transportOrder) {
        transportOrder.setCreateTime(LocalDateTime.now());
        transportOrder.setId(idGenerator.nextId(transportOrder) + "");
        transportOrder.setStatus(TransportOrderStatus.CREATED.getCode());
        transportOrder.setSchedulingStatus(TransportOrderSchedulingStatus.TO_BE_SCHEDULED.getCode());
        save(transportOrder);
        return transportOrder;
    }

    @Override
    public IPage<TransportOrder> findByPage(Integer page, Integer pageSize, String orderId, Integer status, Integer schedulingStatus) {
        Page<TransportOrder> iPage = new Page(page, pageSize);
        LambdaQueryWrapper<TransportOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(orderId)) {
            lambdaQueryWrapper.like(TransportOrder::getOrderId, orderId);
        }
        if (status != null) {
            lambdaQueryWrapper.eq(TransportOrder::getStatus, status);
        }
        if (schedulingStatus != null) {
            lambdaQueryWrapper.eq(TransportOrder::getSchedulingStatus, schedulingStatus);
        }
        return page(iPage, lambdaQueryWrapper);
    }

    @Override
    public List<TransportOrder> findAll(List<String> ids, String orderId, Integer status, Integer schedulingStatus) {
        LambdaQueryWrapper<TransportOrder> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (ids != null && ids.size() > 0) {
            lambdaQueryWrapper.in(TransportOrder::getId, ids);
        }
        if (StringUtils.isNotEmpty(orderId)) {
            lambdaQueryWrapper.like(TransportOrder::getOrderId, orderId);
        }
        if (status != null) {
            lambdaQueryWrapper.eq(TransportOrder::getStatus, status);
        }
        if (schedulingStatus != null) {
            lambdaQueryWrapper.eq(TransportOrder::getSchedulingStatus, schedulingStatus);
        }
        return list(lambdaQueryWrapper);
    }

    @Override
    public TransportOrder findByOrderId(String orderId) {
        return getOne(new LambdaQueryWrapper<TransportOrder>().eq(TransportOrder::getOrderId, orderId));
    }



    @Override
    public List<TransportOrderVO> queryTransportOrderByTransportTaskId(String id) {
        /*List<TransportOrderVO> transportOrderVOS = transportOrderMapper.TransportOrderMapper(id);
        transportOrderVOS.forEach((transportOrder)->{
            String orderId = transportOrder.getOrderId();
            //TODO 通过订单id查询取件派件任务
            QueryWrapper<TaskPickupDispatch> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(TaskPickupDispatch::getOrderId,orderId).eq(TaskPickupDispatch::getTaskType,1);
            TaskPickupDispatch taskPickup = taskPickupDispatchMapper.selectOne(wrapper);
            TaskPickupDispatchDTO taskPickupDTO = new TaskPickupDispatchDTO();
            if(!ObjectUtils.isEmpty(taskPickup)){
                BeanUtils.copyProperties(taskPickup,taskPickupDTO);
            }
            transportOrder.setTaskPickupDTO(taskPickupDTO);

            QueryWrapper<TaskPickupDispatch> dispatchQueryWrapper = new QueryWrapper<>();
            dispatchQueryWrapper.lambda().eq(TaskPickupDispatch::getOrderId,orderId).eq(TaskPickupDispatch::getTaskType,2);
            TaskPickupDispatch taskDispatch = taskPickupDispatchMapper.selectOne(dispatchQueryWrapper);
            TaskPickupDispatchDTO taskDispatchDTO = new TaskPickupDispatchDTO();
            if(!ObjectUtils.isEmpty(taskDispatch)){
                BeanUtils.copyProperties(taskDispatch,taskPickupDTO);
            }
            transportOrder.setTaskDispatchDTO(taskDispatchDTO);

        });
        return transportOrderVOS;*/
        List<TransportOrderVO> transportOrderVOS = transportOrderMapper.TransportOrderMapper(id);
        transportOrderVOS.forEach(transportOrder -> {
            String orderId = transportOrder.getOrderId();
            //TODO 通过订单id查询取件派件任务
            QueryWrapper<TaskPickupDispatch> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(TaskPickupDispatch::getOrderId,orderId).eq(TaskPickupDispatch::getTaskType,1);
            TaskPickupDispatch taskPickup = taskPickupDispatchMapper.selectOne(wrapper);
            TaskPickupDispatchDTO taskPickupDTO = new TaskPickupDispatchDTO();
            if(!ObjectUtils.isEmpty(taskPickup)){
                BeanUtils.copyProperties(taskPickup,taskPickupDTO);
            }
            transportOrder.setTaskPickupDTO(taskPickupDTO);

            QueryWrapper<TaskPickupDispatch> dispatchQueryWrapper = new QueryWrapper<>();
            dispatchQueryWrapper.lambda().eq(TaskPickupDispatch::getOrderId,orderId).eq(TaskPickupDispatch::getTaskType,2);
            TaskPickupDispatch taskDispatch = taskPickupDispatchMapper.selectOne(dispatchQueryWrapper);
            TaskPickupDispatchDTO taskDispatchDTO = new TaskPickupDispatchDTO();
            if(!ObjectUtils.isEmpty(taskDispatch)){
                BeanUtils.copyProperties(taskDispatch,taskPickupDTO);
            }
            transportOrder.setTaskDispatchDTO(taskDispatchDTO);

            OrderDTO orderDTO = orderFeign.queryById(transportOrder.getOrderId());
            transportOrder.setOrderDTO(orderDTO);

            OrderCargoDto orderCargoDto = cargoFeign.findByOrderId(transportOrder.getOrderId());
            transportOrder.setOrderCargoDto(orderCargoDto);

        });
        return transportOrderVOS;

    }

    /**
     * 运单列表list
     * @param transportOrderVO
     * @return
     */
    @Override
    public List<TransportOrderVO> queryAll(TransportOrderVO transportOrderVO) {
        List<TransportOrderVO> transportOrderVOS = transportOrderMapper.queryTransportOrders(transportOrderVO);
        transportOrderVOS.forEach(transportOrder -> {
            String orderId = transportOrder.getOrderId();
            //TODO 通过订单id查询取件派件任务
            QueryWrapper<TaskPickupDispatch> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(TaskPickupDispatch::getOrderId,orderId).eq(TaskPickupDispatch::getTaskType,1);
            TaskPickupDispatch taskPickup = taskPickupDispatchMapper.selectOne(wrapper);
            TaskPickupDispatchDTO taskPickupDTO = new TaskPickupDispatchDTO();
            if(!ObjectUtils.isEmpty(taskPickup)){
                BeanUtils.copyProperties(taskPickup,taskPickupDTO);
            }
            transportOrder.setTaskPickupDTO(taskPickupDTO);

            QueryWrapper<TaskPickupDispatch> dispatchQueryWrapper = new QueryWrapper<>();
            dispatchQueryWrapper.lambda().eq(TaskPickupDispatch::getOrderId,orderId).eq(TaskPickupDispatch::getTaskType,2);
            TaskPickupDispatch taskDispatch = taskPickupDispatchMapper.selectOne(dispatchQueryWrapper);
            TaskPickupDispatchDTO taskDispatchDTO = new TaskPickupDispatchDTO();
            if(!ObjectUtils.isEmpty(taskDispatch)){
                BeanUtils.copyProperties(taskDispatch,taskPickupDTO);
            }
            transportOrder.setTaskDispatchDTO(taskDispatchDTO);

            OrderDTO orderDTO = orderFeign.queryById(transportOrder.getOrderId());
            transportOrder.setOrderDTO(orderDTO);

            OrderCargoDto orderCargoDto = cargoFeign.findByOrderId(transportOrder.getOrderId());
            transportOrder.setOrderCargoDto(orderCargoDto);

        });
        return transportOrderVOS;
    }
}
